package com.GUI.Lesson04;

import javax.swing.*;
import java.awt.*;
import java.net.URL;

public class ImageIconDemo extends JFrame {
    public ImageIconDemo(){
        JLabel label = new JLabel("ImageIconDemo");
        //获取图片的地址
        URL url = ImageIconDemo.class.getResource("tx.png");

        ImageIcon imageIcon = new ImageIcon(url);//避免重名
        label.setIcon(imageIcon);//设置图片为lable的图标
        label.setHorizontalAlignment(SwingConstants.CENTER);//水平居中
        Container container = getContentPane();//获取面板内容
        container.add(label);//加入标签

        setVisible(true);//可见
        setBounds(20,20,100,100);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//关闭
    }

    public static void main(String[] args) {
        //主程序调用
        new ImageIconDemo();
    }
}
